package com.zxl.live.screen.screen.ui.widget.panel.heart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.ArrayList;
import java.util.List;

public class Garden {
	
	public static final float PETALCOUNT_MIN = 8;
	
	public static final float PETALCOUNT_MAX = 15;
	
	public static final float PETALSTRETCH_MAX = 3;
	
	public static final float PETALSTRETCH_MIN = 0.1f;
	
	public static final float GROWFACTOR_MAX = 1;
	
	public static final float GROWFACTOR_MIN = 0.1f;
	
	public static final float BLOOMRADIUS_MAX = 10;
	
	public static final float BLOOMRADIUS_MIN = 8;
	
	public static final float DENSITY = 10;
	
	public static final float GROWSPEED = 1000f / 60f;
	
	public static final int COLOR_RMIN = 128;
	
	public static final int COLOR_RMAX = 255;
	
	public static final int COLOR_GMIN = 0;
	
	public static final int COLOR_GMAX = 128;
	
	public static final int COLOR_BMIN = 0;
	
	public static final int COLOR_BMAX = 128;
	
	public static final int COLOR_AMIN = 180;
	
	public static final int COLOR_AMAX = 200;
	
	public static final float COLOR_OPACITY = 0.1f;
	
	public static final float TANANGLE = 60;
	
	public static final double CIRCLE = 2 * Math.PI;
	
	private List<Bloom> blooms;

	public Garden() {
		blooms = new ArrayList<Bloom>();
	}

	public void render(Canvas canvas, Paint paint, int alpha) {
		for (int i = 0; i < this.blooms.size(); i++) {
          this.blooms.get(i).draw(canvas, paint, alpha);
		}
	}

	public void add(Bloom bloom) {
		this.blooms.add(bloom);
	}

	public Garden removeBloom(Bloom b) {
		Bloom bloom;
		for (int i = 0; i < this.blooms.size(); i++) {
			bloom = this.blooms.get(i);
			if (bloom == b) {
				this.blooms.remove(i);
				return this;
			}
		}
		return null;
	}

	public void createRandomBloom(float x, float y) {
		this.createBloom(x, y,
				Garden.randomInt(Garden.BLOOMRADIUS_MIN, Garden.BLOOMRADIUS_MAX),
				Garden.randomrgba(Garden.COLOR_RMIN, Garden.COLOR_RMAX, Garden.COLOR_GMIN, Garden.COLOR_GMAX,
					Garden.COLOR_BMIN, Garden.COLOR_BMAX, Garden.COLOR_OPACITY),
				Garden.randomInt(Garden.PETALCOUNT_MIN, Garden.PETALCOUNT_MAX));
	}

	public void createBloom(float x, float y, int r, int c, int pc) {
		new Bloom(new Vector(x, y), r, c, pc, this);
	}
	
	public void clear() {
		this.blooms.clear();
	}
	
	public static int randomInt(float min, float max) {
		return (int) (Math.floor(Math.random() * (max - min + 1)) + min);
	}

	public static float random(float min, float max) {
		 return (float) (Math.random() * (max - min) + min);
	}
	
	
	public static float degrad(float angle) {
		return (float) (CIRCLE / 360f * angle);
	};
	
    public static float raddeg(float angle) {
        return (float) (angle / CIRCLE * 360);
    };
    
    
    public static int rgba(int r, int g, int b, int a) {
    	return Color.argb(a, r, g, b);
    };
    
    public static int randomrgba(int rmin, int rmax, int gmin, int gmax, int bmin, int bmax, float a) {
		int r = Math.round(Garden.random(rmin, rmax));
		int g = Math.round(Garden.random(gmin, gmax));
		int b = Math.round(Garden.random(bmin, bmax));
		int limit = 5;
		if (Math.abs(r - g) <= limit && Math.abs(g - b) <= limit && Math.abs(b - r) <= limit) {
			return Garden.randomrgba(rmin, rmax, gmin, gmax, bmin, bmax, a);
		} else {
			int aa = Math.round(Garden.random(COLOR_AMIN, COLOR_AMAX));
			return Garden.rgba(r, g, b, aa);
		}
    };
}
